package com.situ.object.controll;


import com.fasterxml.jackson.databind.ObjectMapper;
import com.situ.object.common.ResponseBean;
import com.situ.object.entity.Teacher;
import com.situ.object.service.TeacherService;
import com.situ.object.service.impl.TeacherServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/teacher")
public class TeacherServlet extends HttpServlet {
    private TeacherService teacherService = new TeacherServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置req编码格式和resp内容类型
        req.setCharacterEncoding("utf-8");
        resp.setContentType("application/json;charset=utf-8");

        //二、获取参数
        //根据jsp页面的ajax传输地址中的method的属性值进行分支
        String method = req.getParameter("method");
//        System.out.println(method);
        if ("add".equals(method)){
            try {
                add(req,resp);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }else if ("teacherList".equals(method)){
            list(req,resp);
        }else if ("edit".equals(method)){
            edit(req,resp);
        }else if ("delete".equals(method)){
            delete(req,resp);
        }else {
            System.out.println("----------");
        }

    }

    private void delete(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException {
        //获取参数  删除，根据id来删除用户
        Integer goods_id= Integer.valueOf(request.getParameter("teacher_id"));

        try{

            teacherService.deleteTeacher(goods_id);

            //创建jackson的核心对象
            ObjectMapper objectMapper=new ObjectMapper();
            String jsonStr=objectMapper.writeValueAsString(ResponseBean.ok());
            response.getWriter().append(jsonStr);
        }catch (Exception e){
            ObjectMapper objectMapper=new ObjectMapper();
            String jsonStr=objectMapper.writeValueAsString(ResponseBean.failed(e.getMessage()==null?"删除失败":e.getMessage()));
            response.getWriter().append(jsonStr);
        }
    }

    private void edit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取参数  Integer.valueOf 转为int类型
        Integer id = Integer.valueOf(req.getParameter("teacher_id"));
        String name = req.getParameter("teacher_name");
        String password = req.getParameter("teacher_password");
        String sex = req.getParameter("teacher_sex");
        Integer age = Integer.valueOf(req.getParameter("teacher_age"));
        String phone = req.getParameter("phone");
        String title = req.getParameter("title");


        //调用service 首先合成
        Teacher teacher = new Teacher();
        teacher.setTeacherId(id);
        teacher.setTeacherName(name);
        teacher.setStudentPassword(password);
        teacher.setTeacherSex(sex);
        teacher.setTeacherAge(age);
        teacher.setPhone(phone);
        teacher.setTitle(title);

        try {
            //三、
            teacherService.edit(teacher);

            ObjectMapper objectMapper = new ObjectMapper();
            String jsonStr = objectMapper.writeValueAsString(ResponseBean.ok());
            resp.getWriter().append(jsonStr);
        } catch (Exception e) {
            ObjectMapper objectMapper = new ObjectMapper();
            String jsonStr = objectMapper.writeValueAsString(ResponseBean.failed(e.getMessage()==null?"编辑失败":e.getMessage()));
            resp.getWriter().append(jsonStr);
        }

    }

    private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //通过集合接收查询的所有教师的数据
        List<Teacher> list = teacherService.selectAll();
        System.out.println("list"+list);

        //创建 ObjectMapper 对象
        ObjectMapper objectMapper = new ObjectMapper();
        //将 Java 对象序列化为JSON 字符串
        String jsonStr = objectMapper.writeValueAsString(ResponseBean.ok(0,list));
        resp.getWriter().append(jsonStr);
    }

    private void add(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        //一、获取参数
        String name = req.getParameter("teacher_name");
        String password = req.getParameter("teacher_password");
        String sex = req.getParameter("teacher_sex");
        Integer age = Integer.valueOf(req.getParameter("teacher_age"));
        String phone = req.getParameter("phone");
        String title = req.getParameter("title");

        //二、将参数set给用户的实现类对象
        Teacher teacher = new Teacher();
        teacher.setTeacherName(name);
        teacher.setStudentPassword(password);
        teacher.setPhone(phone);
        teacher.setTeacherSex(sex);
        teacher.setTeacherAge(age);
        teacher.setTitle(title);

        //三、调用service 首先合成类
        int i = teacherService.addTeacher(teacher);

        //四、序列化
        if (i > 0){
            ObjectMapper objectMapper = new ObjectMapper();
            String jsonStr = objectMapper.writeValueAsString(ResponseBean.ok());
            resp.getWriter().append(jsonStr);
        }else {
            ObjectMapper objectMapper = new ObjectMapper();
            String jsonStr = objectMapper.writeValueAsString(ResponseBean.failed("添加失败"));
            resp.getWriter().append(jsonStr);
        }
    }


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}

